package com.pluginsacceptor.plugin.chart.impl;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.util.List;
/**
 *
 * @author VadymPivak
 *
 */
public class VueBar implements IVueTableau {

    /** */
    private static final Font FONT = new Font("Verdana", Font.BOLD, 12);

    /** */
    private static Color[] COLOR = new Color[] {
        Color.GREEN,
        Color.MAGENTA,
        Color.YELLOW,
        Color.BLUE,
        Color.RED,
        Color.CYAN
    };

    /** Delay for title name */
    private static int HEIGHT_TITLE = 10;
    /**
     * {@inheritDoc}
     */
    @Override
    public void dessine(final Component c, final Graphics g, final ITableauEntier modele) {

        g.setFont(FONT);
        final Dimension dimension = c.getSize();

        final int numColumn = modele.getLongueur();
        final int maxHight = modele.getMaximum();


        if (numColumn > 0 && maxHight > 0) {
            final int xStep = dimension.width / numColumn;

            final int yStep = dimension.height / maxHight;

            int height, x;
            for (int cnt = 0; cnt < numColumn; cnt++) {
                final List<Double> data = modele.getValeur(cnt);
                final double subColumns =data.size();
                for (int numCol = 0; numCol < subColumns; numCol++) {
                    height = (int) (data.get(numCol) * yStep);
                    x = (int) (cnt * xStep + numCol * xStep / subColumns);
                    drawColumn(
                        g,
                        x,
                        dimension.height - height - HEIGHT_TITLE,
                        (int) (xStep / subColumns - 1),
                        height - HEIGHT_TITLE,
                        COLOR[numCol % COLOR.length]
                    );

                    g.setColor(COLOR[(numCol + 1) % COLOR.length]);
                    final String text = String.valueOf(data.get(numCol));
                    g.drawString(
                        text,
                       (int) (x + xStep / subColumns / 2 - text.length() * 3),
                       dimension.height - height + HEIGHT_TITLE
                    );
                }
                g.setColor(Color.BLACK);
                g.drawString(
                    modele.getTitle().get(cnt),
                    cnt  * xStep + xStep / 2,
                    dimension.height - HEIGHT_TITLE / 2
                );
            }
        }
    }


    /**
     * Draw filling column of diagram
     * @param graphics the Graphics instance
     * @param x the start x coordinate
     * @param y the start y coordinate
     * @param width the width of column
     * @param height the height of column
     * @param color the rectangle color
     */
    private static void drawColumn(
        final Graphics graphics,
        final int x,
        final int y,
        final int width,
        final int height,
        final Color color
    ) {
        graphics.drawRect(x, y, width, height);
        graphics.setColor(color);
        graphics.fillRect(x, y, width, height);
    }
}
